package org.sgx.gwteditors.client.util;

import java.util.LinkedList;
import java.util.List;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.resources.client.TextResource;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.TextArea;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

/**
 * dialog and other general gui utilities
 * 
 * @author sg
 * 
 */
public class GUIUtil {

	public static String formLabelWidth = "120px";

	/**
	 * common method for dispatching rpc / services exceptions
	 * 
	 * @param t
	 */
	public static void dispatchRPCError(Throwable t) {
		dispatchRPCError("RPC Error - Fail to comunicate with server: ", t);
	}

	public static void dispatchRPCError(String msg, Throwable t) {
		com.google.gwt.user.client.Window.alert(msg + " " + t.getLocalizedMessage());
	}

	/**
	 * common method for dispatching rpc / services exceptions
	 * 
	 * @param msg
	 * @param t
	 */
	public static void dispatchServiceError(String msg, Exception t) {
		String s = "Service Error: " + msg + " - " + t.getMessage() + " - " + t;
		Window.alert(s);
		// if(JavaUtil.isDebug())
		// t.printStackTrace();
	}

	// dialogs

	public static DialogBox createDialogBox(String title, Widget content) {
		DialogBox box = new DialogBox();
		box.setText(title);
		// box.setWidth("80%");
		// VerticalPanel p = new VerticalPanel();
		// p.add(content);
		box.add(content);
		return box;
	}

	public static void showMessage(String title, String msg) {
		VerticalPanel p = new VerticalPanel();
		p.add(new Label(msg));
		Button b = new Button("OK");
		p.add(b);
		final DialogBox d = createDialogBox(title, p);
		b.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				d.hide();
			}
		});
		showDialogBox(d);
	}

	public static void showText(String title, String txt) {
		VerticalPanel p = new VerticalPanel();
		TextArea ta = new TextArea();
		ta.setText(txt);
		p.add(ta);
		Button b = new Button("OK");
		p.add(b);
		final DialogBox d = createDialogBox(title, p);
		b.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				d.hide();
			}
		});
		ta.setSize("500px", (getMaxHeight() - 200) + "px");
		showDialogBox(d);
	}

	public static DialogBox acceptCancelDialog(String title, Widget content, AcceptCancelDialogListener l) {
		return acceptCancelDialog(title, content, "Accept", "Cancel", l);
	}

	public static DialogBox acceptDialog(String title, Widget content) {
		return acceptCancelDialog(title, content, "Accept", null, new AcceptCancelDialogListener() {

			@Override
			public void notifyCancel(DialogBox db) {
			}

			@Override
			public void notifyAccept(DialogBox db) {
			}
		});
	}

	public static DialogBox acceptHtmlDialog(String title, TextResource htmlFragment) {
		HTML content = new HTML(htmlFragment.getText());
		return acceptCancelDialog(title, content, "Accept", null, new AcceptCancelDialogListener() {

			@Override
			public void notifyCancel(DialogBox db) {
			}

			@Override
			public void notifyAccept(DialogBox db) {
			}
		});
	}

	public static DialogBox acceptCancelDialog(String title, Widget content, String acceptLabel, String cancelLabel,
			final AcceptCancelDialogListener l) {

		final DialogBox db = new DialogBox();
		db.setText(title);
		VerticalPanel p = new VerticalPanel();
		db.add(p);
		p.add(content);
		FlowPanel fp = new FlowPanel();
		p.add(fp);

		if (acceptLabel != null)
			fp.add(new Button(acceptLabel, new ClickHandler() {
				@Override
				public void onClick(ClickEvent event) {
					db.hide();
					l.notifyAccept(db);
				}
			}));
		if (cancelLabel != null)
			fp.add(new Button(cancelLabel, new ClickHandler() {
				@Override
				public void onClick(ClickEvent event) {
					db.hide();
					l.notifyCancel(db);
				}
			}));
		showDialogBox(db);
		return db;
	}

	public static void showTextEntryDialog(String title, String text, final AcceptCancelDialogListener l) {
		VerticalPanel p = new VerticalPanel();
		p.add(new Label("Enter the text. UseEditor \\n for new lines"));
		final TextArea ta = new TextArea();
		ta.setSize("100%", "200px");
		p.add(ta);

		acceptCancelDialog(title, p, l);
	}

	public static void showDialogBox(DialogBox d) {
		d.setAnimationEnabled(true);
		d.setGlassEnabled(true);
		d.center();
		// d.setWidth("70%");
		d.show();
	}

	public static DialogBox showWidgetDialog(String title, Widget content) {
		DialogBox db = createDialogBox(title, content);
		showDialogBox(db);
		return db;
	}

	public static void showErrorMessage(String title, String s) {
		showMessage(title, s);
	}

	public static void showErrorMessage(String title, Exception e) {
		showErrorMessage(title, e.getMessage());
	}

	// private static GWTCAlert showMessageDialog1Alert=null;
	// public static void showMessageDialog1(String msg) {
	// if(showMessageDialog1Alert==null) {
	// showMessageDialog1Alert=new GWTCAlert();
	// }
	// showMessageDialog1Alert.alert(msg);
	// }

	// misc gui utils

	public static List<String> getSelectedTexts(ListBox l) {
		List<String> labels = new LinkedList<String>();
		for (int i = 0; i < l.getItemCount(); i++) {
			if (l.isItemSelected(i))
				labels.add(l.getItemText(i));
		}
		return labels;
	}

	public static int getMaxHeight() {
		return (Window.getClientHeight() - 20);
	}

	public static String getMaxHeightStr() {
		return getMaxHeight() + "px";
	}

}
